package com.fortune.controller;


import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.constants.enums.Shelf;
import com.fortune.dto.excution.ProductExcution;
import com.fortune.dto.in.product.ProductAddDto;
import com.fortune.dto.in.product.ProductEditDto;
import com.fortune.dto.in.product.ProductListDto;
import com.fortune.dto.in.product.ProductUpAndDown;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.ProductEnum;
import com.fortune.service.MProductInfoService;
import com.fortune.service.MSpecificationService;
import com.fortune.service.MUnitService;
import com.fortune.vo.mSpecification.MSpecification;
import com.fortune.vo.mUnit.MUnit;
import com.fortune.vo.productMgt.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "product", description = "商品管理", tags = {"商品管理相关"})
@RestController
@RequestMapping("product")
public class ProductController {
	@Autowired
	private MProductInfoService mProductInfoService;
	@Autowired
	private MSpecificationService mSpecificationService;
	@Autowired
	private MUnitService mUnitService;

	@ApiOperation(value = "商品列表", nickname = "productList", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
	})
	@GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<Product>> list(HttpServletRequest request,@ApiIgnore @Validated ProductListDto ProductListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		ProductListDto.setPage((ProductListDto.getPage()-1)*10);
		List<Product> productList = mProductInfoService.queryProductInfoList(companyId,ProductListDto);
		int count = mProductInfoService.queryProductInfoListCount(companyId);
		PagingData<Product> pagingData = new PagingData<>();
		pagingData.setData(productList);
		pagingData.setRecordsTotal(count);
		return new ProductExcution<PagingData<Product>>(pagingData, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "添加商品", nickname = "mProductAdd", notes = "" ,tags = {"@石匡"})
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody ProductAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mProductInfoService.queryProductByName(companyId,addDto.getName());
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.queryProductByNumber(companyId,addDto.getNumber());
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.addProduce(companyId,addDto);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "编辑商品", nickname = "mProductEdit", notes = "" ,tags = {"@石匡"})
	@PutMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody ProductEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = queryProductByProduceId(companyId,editDto.getId());
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		num = mProductInfoService.queryEditProductByNameOrNumber(companyId,editDto);
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
//		num = mProductInfoService.queryProductByNumber(companyId,editDto.getNumber());
//		if (num > 0){
//			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
//		}
		num = mProductInfoService.editProductByProduceIdAndProduceId(companyId,editDto);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "删除商品", nickname = "mProductDelete", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParam(name="produceId", value="用户id",paramType="query",dataType="string")
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="用户id不能为空") String produceId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = queryProductByProduceId(companyId,produceId);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		num = mProductInfoService.deleteProduce(companyId,produceId);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "上下架商品", nickname = "mProductUpAndDown", notes = "" ,tags = {"@石匡"})
	@PutMapping(value = "mProductUpAndDown",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> upAndDown(HttpServletRequest request,@Validated @RequestBody ProductUpAndDown upAndDown) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
//		int num = queryProductByProduceId(companyId,upAndDown.getId());
//		if (num < 1){
//			return new ProductExcution(null,ProductEnum.PRODUCE_NO_EXIST);
//		}
		Product product = mProductInfoService.queryProduceInfo(companyId,upAndDown.getId());
		if (product==null){
			return new ProductExcution(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		Shelf shelf = product.getShelf();
		if (shelf.toString().equals(upAndDown.getType().toString()) && upAndDown.getType().toString().equals("YES")){
			return new ProductExcution(null,ProductEnum.UP);
		}
		if (shelf.toString().equals(upAndDown.getType().toString()) && upAndDown.getType().toString().equals("NO")){
			return new ProductExcution(null,ProductEnum.DOWN);
		}
		int num = mProductInfoService.upAndDownProduce(companyId,upAndDown);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	private int queryProductByProduceId(String companyId,String produceId){
		return mProductInfoService.queryProductByProduceId(companyId,produceId);
	}

	@ApiOperation(value = "规格型号", nickname = "mSpecificationList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "mSpecificationList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MSpecification>> mSpecificationList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<MSpecification> list = mSpecificationService.queryProduceMSpecificationList(companyId);
		return new ProductExcution<List<MSpecification>>(list, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "单位", nickname = "mUnitList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "mUnitList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<MUnit>> mUnitList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<MUnit> list = mUnitService.queryProduceMunit(companyId);
		return new ProductExcution<List<MUnit>>(list, ProductEnum.SUCCESS);
	}


}
